package com.oracle.cmp.controller;

import com.oracle.cmp.common.Constant;
import com.oracle.cmp.common.PageInfo;
import com.oracle.cmp.common.Result;
import com.oracle.cmp.service.UserService;
import com.oracle.cmp.service.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/user.do")
public class UserController extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String op = request.getParameter("op");
        if("query".equals(op)){
            query(request,response);
        }else if("delete".equals(op)){
            delete(request,response);
        }else if("edit".equals(op)){
            edit(request,response);
        }else if("add".equals(op)){
            add(request,response);
        }
    }
    public void query(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        UserService userService=new UserServiceImpl();
        String LoginName=request.getParameter("LoginName");
        String UserName=request.getParameter("UserName");
        String pageNo = request.getParameter("pageNum");
        int pageNum = 1;
        if(pageNo!=null)
            pageNum = Integer.parseInt(pageNo);
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("LoginName",LoginName);
        map.put("UserName",UserName);
        Result result = userService.queryForPage(map,pageNum);
        int total =userService.count(map);
        int pages = (total+ Constant.PAGESIZE-1)/Constant.PAGESIZE;
        PageInfo pageInfo = new PageInfo();
        pageInfo.setList((List)result.getData());
        pageInfo.setPageNum(pageNum);
        pageInfo.setPageSize(Constant.PAGESIZE);
        pageInfo.setTotal(total);
        pageInfo.setPages(pages);
        request.setAttribute("pageInfo",pageInfo);
        request.setAttribute("LoginName", LoginName);
        request.setAttribute("UserName", UserName);
        request.getRequestDispatcher("/page/system/user/userlist.jsp").forward(request,response);
    }


    public void delete(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        UserService userService=new UserServiceImpl();
        String userid = request.getParameter("userid");
         userService.delete(Integer.parseInt(userid));
        request.getRequestDispatcher("/user.do?op=query").forward(request,response);
    }


    public void edit(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {

    }
    public void add(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/page/system/user/useradd.jsp").forward(request,response);
    }

}
